package com.fanshuai.spring.boot.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;

@Configuration
public class DBConfig {
    @Value("${db.test.url}")
    private String urlTest;

    @Value("${db.test.user}")
    private String usernameTest;

    @Value("${db.test.password}")
    private String passwordTest;

    @Value("${db.test2.url}")
    private String urlTest2;

    @Value("${db.test2.user}")
    private String usernameTest2;

    @Value("${db.test2.password}")
    private String passwordTest2;

    @Value("${druid.xa.enabled:false}")
    private boolean druidXAEnabled;

    @Bean
    @Primary
    public DruidDataSource dataSource1() {
        return getDataSource(urlTest, usernameTest, passwordTest);
    }

    @Bean(name = "dataSource2")
    public DruidDataSource dataSource2() {
        return getDataSource(urlTest2, usernameTest2, passwordTest2);
    }

    @Bean
    public JdbcTemplate template1(DruidDataSource dataSource1) {
        return new JdbcTemplate(dataSource1);
    }

    @Bean
    public JdbcTemplate template2(@Qualifier("dataSource2") DruidDataSource dataSource2) {
        return new JdbcTemplate(dataSource2);
    }

    @Bean
    @Primary
    public DataSourceTransactionManager transactionManager(DataSource dataSource1) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource1);
        return transactionManager;
    }

    @Bean(name = "transactionManager2")
    public DataSourceTransactionManager transactionManager2(@Qualifier("dataSource2") DataSource dataSource2) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource2);
        return transactionManager;
    }

    private DruidDataSource getDataSource(String url, String username, String password) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        dataSource.setMaxActive(8);
        dataSource.setMinIdle(5);
        dataSource.setMaxWait(1000);
        dataSource.setTimeBetweenEvictionRunsMillis(1000);

        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(true);
        dataSource.setTestWhileIdle(true);

        return dataSource;
    }
}
